﻿// See https://aka.ms/new-console-template for more information
// 假设我们有一个需要消耗大量资源的图片加载类 RealImage，我们可以通过代理模式来延迟加载该图片。
Console.WriteLine("Hello, World!");
IImage image = new ProxyImage("test_image.jpg");
// 图片将会在第一次调用 Display() 时加载
image.Display();
// 后续调用不会再加载图片
image.Display();


// 图像接口
public interface IImage
{
    void Display();
}

// 真实图像类
public class RealImage : IImage
{
    private readonly string _fileName;

    public RealImage(string fileName)
    {
        _fileName = fileName;
        LoadFromDisk();
    }

    private void LoadFromDisk()
    {
        Console.WriteLine("Loading " + _fileName);
    }

    public void Display()
    {
        Console.WriteLine("Displaying " + _fileName);
    }
}

// 代理图像类
public class ProxyImage : IImage
{
    private readonly string _fileName;
    private RealImage _realImage;

    public ProxyImage(string fileName)
    {
        _fileName = fileName;
    }

    public void Display()
    {
        if (_realImage == null)
        {
            _realImage = new RealImage(_fileName);
        }
        _realImage.Display();
    }
}